package com.open.capacity.paper.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.model.Question;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.paper.service.QuestionService;
import com.open.capacity.paper.vo.PaperQuestionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author liangd
 * @since 2021-02-02 15:16
 */
@Api(tags = "问题管理")
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 模糊查询
     */
    @GetMapping("/getQuestions")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @ApiOperation(value = "后台管理查询所有的问题标识")
    @LogAnnotation(module = "paper-center")
    public PageResult<Question> getQuestions(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
            return questionService.getQuestions(params);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 添加或修改
     */
    @PostMapping("/saveOrUpdate")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理新增或者更新问题标识,optnid==null?新增:更新")
    public Result saveOrUpdate(@RequestBody Question question) throws ControllerException {
        try {
            if (question.getQuesid() != null) {
                return questionService.updateQuestion(question);
            } else {
                return questionService.saveQuestion(question);
            }
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 逻辑删除
     */
    @DeleteMapping("/delete/{quesid}")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理删除问题标识")
    public Result deleteQuestion(@PathVariable Integer quesid) throws ControllerException {
        try {
            return questionService.deleteQuestion(quesid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 根据papeid查询所有问题
     */
    @GetMapping("/{papeid}/getAllQuestions")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理根据papeid查询所有问题标识")
    public Result getAllQuestions(@PathVariable Integer papeid) throws ControllerException {
        try {
            return questionService.getAllQuestions(papeid);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 给问卷添加问题
     */
    @PostMapping("/addQuestions")
    @LogAnnotation(module = "paper-center")
    @ApiOperation(value = "后台管理给问卷添加问题标识")
    public Result addQuestions(@RequestBody PaperQuestionVo paperQuestionVo) throws ControllerException {
        try {
            return questionService.addQuestions(paperQuestionVo.getPapeid(), paperQuestionVo.getQuestIds());
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 导出问题
     */
    @PostMapping("/exportQuestion")
    public void exportQuestion(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws ControllerException {
        try {
            questionService.exportQuestion(params, request, response);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }
}
